//
//  PPFavouritesTimelineContentView.m
//  pickpic
//
//  Created by GaoYongyue on 16/8/27.
//  Copyright © 2016年 MicroDream. All rights reserved.
//

#import "PPFavouritesTimelineContentView.h"
#import "PPFavouriteModel.h"
#import "PPNetworkService.h"
#import "PPFavouritePictureModel.h"
#import "PPProfileViewController.h"
#import "PPTimelinePictureViewController.h"

#import "PPPictureLoadingProgressView.h"

static const CGFloat kFavouritesProfileViewHeight = 46.f;
static const CGFloat kDescLabelVPadding = 14.f;
static const CGFloat kAvatarViewSize = 36.f;
static const NSInteger kDescLabelFont = 15;
static const CGFloat kContainerHPadding = 15.f;

@interface PPFavouritesTimelineContentView ()
@property (weak, nonatomic) IBOutlet UIImageView *avatarView;
@property (weak, nonatomic) IBOutlet UILabel *nickLabel;
@property (weak, nonatomic) IBOutlet UILabel *dateLabel;
@property (weak, nonatomic) IBOutlet UIButton *favouriteButton;
@property (weak, nonatomic) IBOutlet UIImageView *coverImageView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *coverImageHeight;


@property (weak, nonatomic) IBOutlet UIView *coverBottomMaskView;
@property (weak, nonatomic) IBOutlet UILabel *albumTitleLabel;
@property (weak, nonatomic) IBOutlet UILabel *picutresCountLabel;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *descLabelHeight;
@property (weak, nonatomic) IBOutlet UILabel *descLabel;

@property (readwrite, nonatomic, strong) PPPictureLoadingProgressView *progressView;
@end
@implementation PPFavouritesTimelineContentView

- (void)awakeFromNib {
    [super awakeFromNib];

    _avatarView.layer.masksToBounds = YES;
    _avatarView.layer.cornerRadius = kAvatarViewSize/2.f;

    _coverImageView.clipsToBounds = YES;

    [_coverImageView setActionBlock:^(id sender) {


    } forEvent:UIControlEventTouchUpInside];

    __weak typeof(self)weakSelf = self;
    [_avatarView setActionBlock:^(UIControl *control) {
        [weakSelf portraitDidPress];
    } forEvent:UIControlEventTouchUpInside];

    [_nickLabel setActionBlock:^(UIControl *control) {
        [weakSelf portraitDidPress];
    } forEvent:UIControlEventTouchUpInside];

    _progressView = [[PPPictureLoadingProgressView alloc] init];
    _progressView.trackColor = [UIColor colorWithRGBHex:0xd9d9d9];
    _progressView.valueColor = [UIColor colorWithRGBHex:0xc5c5c5];
    [_coverImageView addSubview:_progressView];

    [_progressView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(60));
        make.height.equalTo(@(60));
        make.center.equalTo(_coverImageView);
    }];
}

- (void)portraitDidPress {
    [MobClick event:@"142"];
    PPProfileViewController *profileViewConroller = [[PPProfileViewController alloc] init];
    profileViewConroller.userID = _album.userID;
    [self.delegateViewController.navigationController pushViewController:profileViewConroller animated:YES];
}

- (IBAction)favouriteButtonDidPress:(id)sender {
    if(_favouriteButton.selected) {
        //取消收藏
        [MobClick event:@"143"];
        __weak typeof(self)weakSelf = self;
        UIAlertController *alertContoller = [UIAlertController alertControllerWithTitle:nil message:@"确认取消收藏？" preferredStyle:UIAlertControllerStyleAlert];

        [alertContoller addAction:[UIAlertAction actionWithTitle:@"再想想" style:UIAlertActionStyleCancel handler:^(UIAlertAction *_Nonnull action) {

        }]];
        [alertContoller addAction:[UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction *_Nonnull action) {
            MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:weakSelf.delegateViewController.view animated:YES];
            [PPNetworkService unFavouriteAlbumID:weakSelf.album.albumID version:nil callback:^(id data, NSError *error) {
                [hud hide:YES];

                if(data) {
                    weakSelf.favouriteButton.selected = !weakSelf.favouriteButton.selected;
                    weakSelf.album.isFavorited = @(weakSelf.favouriteButton.selected);

                    if(weakSelf.delegateViewController && [weakSelf.delegateViewController respondsToSelector:@selector(favouriteContentView:album:favouriteShipDidChanged:)]) {
                        [weakSelf.delegateViewController favouriteContentView:weakSelf album:weakSelf.album favouriteShipDidChanged:@(weakSelf.album.isFavorited)];
                    }

                    [weakSelf.delegateViewController.view toastMessage:@"操作成功" duration:1.f];
                } else {
                    [weakSelf.delegateViewController.view toastMessage:error.networkPrompt duration:1.f];
                }
            }];
        }]];
        [_delegateViewController presentViewController:alertContoller animated:YES completion:nil];
    } else {
        //收藏
        __weak typeof(self)weakSelf = self;
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:_delegateViewController.view animated:YES];
        [PPNetworkService favouriteAlbumID:_album.albumID version:nil callback:^(id data, NSError *error) {
            [hud hide:YES];

            if(data) {
                weakSelf.favouriteButton.selected = !weakSelf.favouriteButton.selected;
                weakSelf.album.isFavorited = @(weakSelf.favouriteButton.selected);

//                if(weakSelf.delegateViewController && [weakSelf.delegateViewController respondsToSelector:@selector(favouriteContentView:albumID:favouriteShipDidChanged:)]) {
//                    [weakSelf.delegateViewController favouriteContentView:weakSelf albumID:weakSelf.album.albumID favouriteShipDidChanged:@(weakSelf.album.isFavorited)];
//                }

                [weakSelf.delegateViewController.view toastMessage:@"操作成功" duration:1.f];
            } else {
                [weakSelf.delegateViewController.view toastMessage:error.networkPrompt duration:1.f];
            }
        }];
    }
}

- (void)setAlbum:(PPAlbumObject *)album {
    _album = album;

    _progressView.hidden = YES;

    if(album.ownerAvatar.length > 0) {
        [album.ownerAvatar applyToImageView:_avatarView];
    } else {
        _avatarView.image = [UIImage imageNamed:@"icon-person-70"];
    }

    _nickLabel.text = album.ownerName;

    _dateLabel.text = [album.updateDate timeAgoForTimeline];

    _favouriteButton.selected = album.isFavorited;

    //audit代表审核信息 //1:通过;2:未通过;4:审核中;8:未审核
    if(album.audit == 2) {
        _coverImageView.image = [UIImage imageNamed:@"ban-2"];
        _coverImageView.contentMode = UIViewContentModeCenter;
        [_coverImageView setActionBlock:nil forEvent:UIControlEventTouchUpInside];
    } else {
        _coverImageView.contentMode = UIViewContentModeScaleAspectFill;

        __weak typeof(self) weakSelf = self;

        NSString *URLString = album.existingCoverURLString;

        if(URLString.length > 0) {
            weakSelf.progressView.hidden = NO;

            [URLString applyToImageView:_coverImageView placeholder:nil progressive:^(double progress) {
                weakSelf.progressView.progress = progress;
            } completion:^{
                weakSelf.progressView.hidden = YES;
            }];
        }

        [_coverImageView setActionBlock:^(UIControl *control) {
            [MobClick event:@"144"];
            PPAlbumObject *album = weakSelf.album;

            if(album.pictures.count == 0) {
                [weakSelf.delegateViewController.navigationController.view toastMessage:@"图册暂时还没有图片哦"];
                return;
            }

            PPTimelinePictureViewController *pictureViewController = [[PPTimelinePictureViewController alloc] init];
            pictureViewController.pictures = album.pictures;
            pictureViewController.ownerID = album.userID;
            pictureViewController.albumTitle = album.title;
            [weakSelf.delegateViewController.navigationController pushViewController:pictureViewController animated:YES];
        } forEvent:UIControlEventTouchUpInside];
    }

    _coverImageHeight.constant = [UIScreen mainScreen].bounds.size.width - 2*kContainerHPadding;

    _albumTitleLabel.text = album.title;
    _picutresCountLabel.text = [NSString stringWithFormat:@"%ld张", album.pictures.count];
    _descLabel.text = album.desc;

    if([album.desc length] == 0) {
        _descLabelHeight.constant = 0;
    } else {
        CGSize descSize = [album.desc sizeOfFont:[UIFont systemFontOfSize:kDescLabelFont] constrainedToSize:CGSizeMake([UIScreen mainScreen].bounds.size.width - 2*kContainerHPadding - 2*kContainerHPadding, CGFLOAT_MAX)];
        CGFloat lineHeight = [album.desc lineHeightOfFont:[UIFont systemFontOfSize:kDescLabelFont]];
        _descLabel.numberOfLines = ceil(descSize.height/lineHeight);
        _descLabelHeight.constant = ceil(descSize.height);
    }
}

+ (CGFloat)heightForAlbum:(PPAlbumObject *)album {
    CGFloat height = kFavouritesProfileViewHeight;

    CGFloat coverViewHeight = [UIScreen mainScreen].bounds.size.width - 2*kContainerHPadding;

    height += coverViewHeight;

    if([album.desc length] == 0) {
        height += kDescLabelVPadding;
    } else {
        CGSize descSize = [album.desc sizeOfFont:[UIFont systemFontOfSize:kDescLabelFont] constrainedToSize:CGSizeMake([UIScreen mainScreen].bounds.size.width - 2*kContainerHPadding - 2*kContainerHPadding, CGFLOAT_MAX)];

        CGFloat descLabelHeight = ceil(descSize.height);
        height += descLabelHeight;
        height += (2*kDescLabelVPadding);
    }

    return height;
}

@end
